package com.rt.schedulenew.service.impl;

import com.rt.schedulebase.dto.*;
import com.rt.schedulebase.entity.ExamApplyOrgan;
import com.rt.schedulebase.entity.ExamScheduleItem;
import com.rt.schedulebase.mapper.ExamApplyItemMapper;
import com.rt.schedulebase.mapper.ExamApplyMapper;
import com.rt.schedulebase.mapper.ExamScheduleItemMapper;
import com.rt.schedulenew.api.*;
import com.rt.schedulenew.dto.ResponseDto;
import com.rt.schedulenew.utils.base.CommonUtil;
import com.rt.schedulenew.utils.base.ConstantsUtil;
import com.rt.schedulenew.utils.global.BusinessException;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulenew.utils.util.DateUtil;
import com.rt.schedulenew.utils.util.QrCodeUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/***
 * 自助机操作相关逻辑
 */
@Service
public class SelfTerminalService implements ISelfTerminalService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ExamApplyMapper examApplyMapper;
    @Autowired
    private ExamApplyItemMapper examApplyItemMapper;
    @Autowired
    private IExamApplyService examApplyService;
    @Autowired
    private IDictHospitalService dictHospitalService;
    @Autowired
    private IExamApplyPatientService examApplyPatientService;
    @Autowired
    private ICommonService commonService;
    @Autowired
    private IDictTerminalService dictTerminalService;
    @Autowired
    private IConfigService configService;
    @Autowired
    private IDictExamQueueService dictExamQueueService;
    @Autowired
    private ExamScheduleItemMapper examScheduleItemMapper;
    @Autowired
    private IExamScheduleActService examScheduleActService;
    @Autowired
    private IExamApplyOrganService examApplyOrganService;

    /***
     * 获取院区列表
     * @param dto
     * @return
     */
    @Override
    public List<DictHospitalDto> getHospitals(DictHospitalDto dto) throws BusinessException {
        if (StringUtils.isBlank(dto.getMergeNo()) && StringUtils.isNotBlank(dto.getApplyNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }

        // 判断是否有申请单号 或者调用来源是移动端或者自助机端
        boolean regex = (StringUtils.isNotBlank(dto.getApplyNo()) || StringUtils.isNotBlank(dto.getMergeNo()))
                && (ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType())
                || ConstantsUtil.TerminalType.MOBILE.getType().equals(dto.getTerminalType()));
        if (regex) {

            ExamApplyDto examApplyDto = new ExamApplyDto();
            examApplyDto.setApplyNo(dto.getApplyNo());
            examApplyDto.setMergeNo(dto.getMergeNo());
            examApplyDto.setTerminalType(dto.getTerminalType());
            // 获取申请单信息
            ExamApplyDto ea = commonService.getApplyForScheduleItem(examApplyDto);
            // 构建申请单预约信息
            ea = commonService.getScheduleToExamApply(examApplyDto, ea);

            if (StringUtils.isNotBlank(ea.getExamStatus())) {
                ea.setScheduleStatus(ea.getExamStatus());
            }
            //
            if (StringUtils.isNotBlank(ea.getDesc())) {
                throw new BusinessException(ea.getDesc());
            }
            // 是否已预约
            String isSchedule = commonService.isScheduleByFlags(ea.getItems(), dto.getTerminalType(), ea.getChargeType());
            if (StringUtils.isNotBlank(isSchedule)) {
                throw new BusinessException(ea.getDesc());
            }
            // 自助机限制
            String selfActLimit = configService.getConfigByName(ConstantsUtil.ConfigName.SELF_ACT_LIMIT.getKey());
            if (StringUtils.isNotBlank(selfActLimit)
                    && StringUtils.isNotBlank(ea.getScheduleStatus())
                    && "08".equals(ea.getScheduleStatus())) {
                String isLimitSelfOperate = configService.isLimitSelfOperate(dto.getTerminalType(), ConstantsUtil.ActType.UPDATE.getType(), "", ea);
                if (StringUtils.isNotBlank(isLimitSelfOperate)) {
                    throw new BusinessException(isLimitSelfOperate);
                }
            }
            ea.setTerminalType(dto.getTerminalType());

            String scheduleLimit = configService.scheduleLimit2(ea);
            if (StringUtils.isNotBlank(scheduleLimit)) {
                throw new BusinessException(scheduleLimit);
            }
        }

        List<DictHospitalDto> hospitalList = dictHospitalService.getHospitalList(dto);
        List<DictHospitalDto> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(hospitalList)) {
            list = hospitalList;
            // 字段转换
            for (DictHospitalDto hospitalDto : list) {
                hospitalDto.setCode(hospitalDto.getHospitalCode());
                hospitalDto.setName(hospitalDto.getHospitalName());
                hospitalDto.setEnabled(hospitalDto.getIsEnable());
                hospitalDto.setAlias(hospitalDto.getHospitalAlias());
                hospitalDto.setAddress(hospitalDto.getMailingAddress());
            }
        }
        return list;
    }


    /**
     * 获取患者信息
     *
     * @param dto
     * @return
     */
    @Override
    public JsonResult getPatientInfo(ExamApplyPatientDto dto) {
        if (StringUtils.isNotBlank(dto.getTerminalType())
                && ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType())
                && StringUtils.isNotBlank(dto.getDeviceId())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (terminalDto == null) {
                return JsonResultUtil.failure("1", "自助机配置信息不存在");
            }
            if (StringUtils.isNotBlank(dto.getIcCard())) {
                String icCard = dto.getIcCard();
                dto.setCommonCard(icCard);
                dto.setIcCard(null);
                dto.setSickId(null);
                if (StringUtils.isNotBlank(terminalDto.getIcCardInputSupportType())) {
                    String icCardInputSupportType = terminalDto.getIcCardInputSupportType();
                    if ("common_card".equals(icCardInputSupportType)) {
                        dto.setCommonCard(icCard);
                    } else if ("sick_id".equals(icCardInputSupportType)) {
                        dto.setSickId(icCard);
                        dto.setCommonCard(null);
                    } else if ("ic_card".equals(icCardInputSupportType)) {
                        dto.setIcCard(icCard);
                        dto.setCommonCard(null);
                    } else if ("inpatient_no".equals(icCardInputSupportType)) {
                        dto.setInpatientNo(icCard);
                    } else if ("outpatient_no".equals(icCardInputSupportType)) {
                        dto.setOutpatientNo(icCard);
                    } else if ("apply_no".equals(icCardInputSupportType)) {
                        dto.setApplyNo(icCard);
                    } else if ("identity_card".equals(icCardInputSupportType)) {
                        dto.setIdentityCard(icCard);
                    } else if ("mi_card".equals(icCardInputSupportType)) {
                        dto.setMiCard(icCard);
                    } else if ("health_card".equals(icCardInputSupportType)) {
                        dto.setHealthCard(icCard);
                    }
                }
            } else {
                dto.setIcCard(null);
            }
        }
        JsonResult jsonResult = examApplyPatientService.getPatientInfo(dto);
        if (jsonResult != null) {
            return jsonResult;
//            if ("0".equals(jsonResult.getStatus()) && jsonResult.getResult() != null) {
//                return JsonResultUtil.success();
//                return XmlUtil.object2xmlResponse("Patient", jsonResult.getResult(), "0", jsonResult.getMessage());
//            }
//            if (jsonResult.getResult() == null) {
//                return XmlUtil.object2xmlResponse("1", "没有查询到您的" + (StringUtils.isBlank(dto.getExamClass()) ? "" : dto.getExamClass()) + "检查申请,如有疑问请到护士站咨询!");
//            }
//            if (StringUtils.isNotBlank(jsonResult.getMessage())) {
//                return XmlUtil.object2xmlResponse("1", jsonResult.getMessage());
//            }
        }
//        return XmlUtil.object2xmlResponse("1", "没有查询到您的" + (StringUtils.isBlank(dto.getExamClass()) ? "" : dto.getExamClass()) + "检查申请,如有疑问请到护士站咨询!");

        return JsonResultUtil.failure("1", "没有查询到您的" + (StringUtils.isBlank(dto.getExamClass()) ? "" : dto.getExamClass()) + "检查申请,如有疑问请到护士站咨询!");
    }


    /**
     * 获取合并申请单号
     *
     * @param examApplyItemDto
     * @return
     */
    public String getMergeNos(ExamApplyItemDto examApplyItemDto) {
        List<ExamApplyItemDto> itemDtoList = examApplyItemMapper.getList(examApplyItemDto);
        if (itemDtoList != null && !itemDtoList.isEmpty()) {
            commonService.handItemCode(itemDtoList, "");
            StringBuffer mergeNos = new StringBuffer();
            for (ExamApplyItemDto itemDto : itemDtoList) {
                if (itemDto.getItNo() != null) {
                    mergeNos.append("," + itemDto.getApplyNo() + "-" + itemDto.getItNo());
                } else {
                    mergeNos.append("," + itemDto.getApplyNo() + "-" + itemDto.getItemNo());
                }
            }
            if (StringUtils.isNotBlank(mergeNos.toString())) {
                return mergeNos.substring(1);
            }
        }
        return "";
    }


    /**
     * 获取患者检查申请单列表信息
     *
     * @param dto
     * @return
     */
    public List<ResponseDto> getPatientApplies(ExamApplyDto dto) throws BusinessException {

        if (StringUtils.isBlank(dto.getPlanMode())) {
            dto.setPlanMode("0");
        }
        dto.setHistoryFlag("1");

        if (StringUtils.isNotBlank(dto.getTerminalType())
                && ConstantsUtil.TerminalType.SELF.getType().equals(dto.getTerminalType())
                && StringUtils.isNotBlank(dto.getDeviceId())) {
            DictTerminalDto dictTerminalDto = new DictTerminalDto();
            dictTerminalDto.setIpAddress(dto.getDeviceId());
            // 获取自助机信息
            DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
            if (Objects.isNull(terminalDto)) {
                throw new BusinessException("自助机配置信息不存在");
            }
            convertCardType4Self(dto, terminalDto);
        }
        // 真实查询
        JsonResult jsonResult = examApplyPatientService.getPatientApplyList(dto);
        if (jsonResult == null || jsonResult.getResult() == null) {
            throw new BusinessException("您的检查申请单没有要检查的项目，如有疑问请到护士站咨询");
        }
        if (!"0".equals(jsonResult.getStatus())) {
            throw new BusinessException(jsonResult.getMessage());
        }
        List<ExamApplyDto> examApplyDtoList = (List<ExamApplyDto>) jsonResult.getResult();
        if (CollectionUtils.isEmpty(examApplyDtoList)) {
            throw new BusinessException("您的检查申请单没有要检查的项目，如有疑问请到护士站咨询");
        }
        // 组装移动端返回数据
        List<ResponseDto> responseDtoList = new ArrayList<ResponseDto>();
        for (ExamApplyDto examApplyDto : examApplyDtoList) {
            int scheduleStatus = StringUtils.isNotBlank(examApplyDto.getScheduleStatus()) ? Integer.parseInt(examApplyDto.getScheduleStatus()) : 0;
            if (scheduleStatus < 3) {
                ResponseDto responseDto = resultToNode(examApplyDto, dto.getTerminalType(), dto.getApplication());
                responseDtoList.add(responseDto);
            } else {
                if (!ConstantsUtil.TerminalType.MOBILE.getType().equals(dto.getTerminalType())) {
                    continue;
                }
                ResponseDto responseDto = resultToNode(examApplyDto, dto.getTerminalType(), dto.getApplication());
                responseDtoList.add(responseDto);
            }
        }
        if (responseDtoList.size() > 0) {
            return responseDtoList;
        }
        throw new BusinessException("您的检查申请单没有要检查的项目，如有疑问请到护士站咨询");
    }


    private ResponseDto resultToNode(ExamApplyDto applyDto, String terminalType, String application) {
        return resultToNode(applyDto, terminalType, application, "");
    }

    private ResponseDto resultToNode(ExamApplyDto applyDto, String terminalType, String application, String deviceId) {
        String reminderType = "2".equals(applyDto.getScheduleStatus()) ? "SCHEDULE" : "SIGN";
        if (StringUtils.isBlank(applyDto.getMergeNo())) {
            StringBuffer applyNos = new StringBuffer();
            List<ExamApplyDto> applyDtoList = examApplyMapper.findByApplyNo(applyDto);
            if (applyDtoList != null && !applyDtoList.isEmpty()) {
                for (ExamApplyDto examApply : applyDtoList) {
                    applyNos.append("," + examApply.getApplyNo() + "-" + examApply.getItemNo());
                }
            }
            applyDto.setMergeNo(applyNos.substring(1));
        }
        applyDto.setType(reminderType);
        applyDto.setTerminalType(terminalType);
        JsonResult jsonResult = examApplyPatientService.getExamApplyInfo(applyDto);
        ExamApplyDto examApplyInfo = (ExamApplyDto) jsonResult.getResult();
        ResponseDto responseDto = new ResponseDto();
        BeanUtils.copyProperties(examApplyInfo, responseDto);
        responseDto.setCanSchedule("1");
        responseDto.setScheduleStatus(StringUtils.isNotBlank(examApplyInfo.getScheduleStatus()) ? examApplyInfo.getScheduleStatus() : "0");
        if (StringUtils.isNotBlank(examApplyInfo.getExamStatus())) {
            if ("10".equals(examApplyInfo.getExamStatus())) {
                responseDto.setScheduleStatus("3");
            } else {
                responseDto.setScheduleStatus("4");
            }
        }
        responseDto.setScheduleStatus(StringUtils.isNotBlank(examApplyInfo.getExamStatus()) ? ("10".equals(examApplyInfo.getExamStatus()) ? "3" : responseDto.getScheduleStatus()) : responseDto.getScheduleStatus());
        ResponseDto.ScheduleHospital scheduleHospital = new ResponseDto.ScheduleHospital();
        scheduleHospital.setCode(examApplyInfo.getScheduleHospital());
        scheduleHospital.setName(examApplyInfo.getScheduleHospitalName());
        scheduleHospital.setLongitude(examApplyInfo.getLongitude());
        scheduleHospital.setLatitude(examApplyInfo.getLatitude());
        responseDto.setScheduleHospital(scheduleHospital);
        responseDto.setScheduleHospitalName(examApplyInfo.getScheduleHospitalName());
        if ("2".equals(responseDto.getScheduleStatus())) {
            responseDto.setQueueName(examApplyInfo.getQueueName());
            responseDto.setLocation(examApplyInfo.getSignLocation());
            responseDto.setZoneName(examApplyInfo.getSignZoneName());
        } else {
            responseDto.setQueueName(examApplyInfo.getExamQueueName());
            responseDto.setLocation(examApplyInfo.getLocation());
            responseDto.setZoneName(examApplyInfo.getZoneName());
            responseDto.setExamQueue(examApplyInfo.getExamQueueName());
            responseDto.setExamApm(examApplyInfo.getExamApm());
        }
        DictExamQueueDto deqDto = new DictExamQueueDto();
        deqDto.setExamClass(responseDto.getExamClass());
        deqDto.setQueueName(responseDto.getQueueName());
        String queueAlias = dictExamQueueService.getQueueAlias(deqDto);
        responseDto.setQueueName(queueAlias);
        responseDto.setWaitCount(applyDto.getWaitCount());
        responseDto.setExamNo(applyDto.getExamNo());
        responseDto.setExamItem(examApplyInfo.getExamItems());
        responseDto.setNotice(examApplyInfo.getNotices());
        responseDto.setReminder(examApplyInfo.getReminders());
        responseDto.setMergeNo(applyDto.getMergeNo());
        responseDto.setTimeEarliest(StringUtils.isNotBlank(applyDto.getTimeEarliest()) ? applyDto.getTimeEarliest() : examApplyInfo.getTimeEarliest());
        responseDto.setTimeSignin(StringUtils.isNotBlank(applyDto.getTimeSignin()) ? applyDto.getTimeSignin() : examApplyInfo.getTimeSignin());
        if (StringUtils.isNotBlank(applyDto.getExamNo())) {
            String cloudFilmUrl = "";
            String no = CommonUtil.toDecimal(Long.parseLong("1" + applyDto.getExamNo()));
            String cloudFilmUrl_tmp = configService.getConfigByName(ConstantsUtil.ConfigName.CLOUD_FILM_URL.getKey());
            if (StringUtils.isNotBlank(cloudFilmUrl_tmp)) {
                String inPatientNo = StringUtils.isNotBlank(applyDto.getInpatientNo()) ? applyDto.getInpatientNo() : "";
                String outPatientNo = StringUtils.isNotBlank(applyDto.getOutpatientNo()) ? applyDto.getOutpatientNo() : "";
                cloudFilmUrl = cloudFilmUrl_tmp.replace("#NO", no).replace("#INPATIENT_NO", inPatientNo).replace("#OUTPATIENT_NO", outPatientNo);
            }
            if (StringUtils.isBlank(application)) {
                cloudFilmUrl = "data:image/png;base64," + QrCodeUtil.creatRrCode(cloudFilmUrl, 150, 150);
            }
            responseDto.setCloudFilmUrl(cloudFilmUrl);
        }
        String isSign = applyDto.getIsSign();
        if (ConstantsUtil.TerminalType.SELF.getType().equals(terminalType)) {
            if (StringUtils.isNotBlank(deviceId)) {
                DictTerminalDto dictTerminalDto = new DictTerminalDto();
                dictTerminalDto.setIpAddress(deviceId);
                DictTerminalDto terminalDto = dictTerminalService.getSelfDeviceInfo(dictTerminalDto);
                if (terminalDto != null) {
                    List<String> unSignQueueList = CommonUtil.split2List(terminalDto.getUnSignQueue());
                    if (unSignQueueList.contains(responseDto.getQueueName())) {
                        isSign = "0";
                    }
                }
            }
            List<String> repeatSignByExamClassList = configService.getConfigListByName(ConstantsUtil.ConfigName.REPEAT_SIGN_BY_EXAM_CLASS.getKey());
            responseDto.setIsRepeatSign("0");
            if ("3".equals(responseDto.getScheduleStatus()) && !repeatSignByExamClassList.isEmpty() && repeatSignByExamClassList.contains(responseDto.getExamClass())) {
                responseDto.setIsRepeatSign("1");
            }
            if (StringUtils.isNotBlank(responseDto.getScheduleStatus())) {
                if (Integer.parseInt(responseDto.getScheduleStatus()) >= 3) {
                    List<String> patLocalIds = new ArrayList<String>();
                    List<String> examNos = new ArrayList<String>();
                    ExamScheduleItem examScheduleItem = new ExamScheduleItem();
                    examScheduleItem.setScheduledId(responseDto.getScheduledId());
                    List<ExamScheduleItem> esiList = examScheduleItemMapper.getExamScheduleItem(examScheduleItem);
                    if (esiList != null && !esiList.isEmpty()) {
                        for (ExamScheduleItem esi : esiList) {
                            if (StringUtils.isNotBlank(esi.getPatLocalId()) && !patLocalIds.contains(esi.getPatLocalId())) {
                                patLocalIds.add(esi.getPatLocalId());
                            }
                            if (StringUtils.isNotBlank(esi.getExamNo()) && !examNos.contains(esi.getExamNo())) {
                                examNos.add(esi.getExamNo());
                            }
                        }
                    }
                    responseDto.setPatLocalIds(patLocalIds);
                    responseDto.setExamNos(examNos);
                }
                if (Integer.parseInt(responseDto.getScheduleStatus()) == 2) {
                    ExamScheduleActDto actDto = new ExamScheduleActDto();
                    actDto.setScheduleId(responseDto.getScheduledId());
                    actDto.setTerminalType(ConstantsUtil.TerminalType.SELF.getType());
                    actDto.setActType("8");
                    int selfPrintCnt = examScheduleActService.findCount(actDto);
                    responseDto.setSelfPrintCnt(String.valueOf(selfPrintCnt));
                }
            }
        }
        responseDto.setIsSign(isSign);
        if (StringUtils.isNotBlank(applyDto.getItemTaboo())) {
            responseDto.setItemTaboo(applyDto.getItemTaboo());
        } else {
            responseDto.setItemTaboo(examApplyInfo.getItemTaboo());
        }
        if (examApplyInfo.getApplyNos() != null && !examApplyInfo.getApplyNos().isEmpty()) {
            com.rt.schedulebase.dto.ExamApplyOrganDto examApplyOrganDto = new com.rt.schedulebase.dto.ExamApplyOrganDto();
            examApplyOrganDto.setApplyNoList(examApplyInfo.getApplyNos());
            List<ExamApplyOrganDto> examApplyOrganList = examApplyOrganService.selectList(examApplyOrganDto);
            ArrayList<ExamApplyOrgan> examApplyOrgans = new ArrayList<>();
            examApplyOrganList.stream().forEach(vo -> {
                ExamApplyOrgan examApplyOrgan = new ExamApplyOrgan();
                BeanUtils.copyProperties(vo,examApplyOrgan);
                examApplyOrgans.add(examApplyOrgan);
            });
            responseDto.setExamOrgan((examApplyOrganList != null && !examApplyOrganList.isEmpty()) ? examApplyOrgans : new ArrayList<ExamApplyOrgan>());
        }
        return responseDto;
    }

    /**
     * 获取自助机支持的卡类型
     *
     * @param dto
     * @param terminalDto
     */
    private void convertCardType4Self(ExamApplyDto dto, DictTerminalDto terminalDto) {
        if (StringUtils.isNotBlank(dto.getIcCard())) {
            String icCard = dto.getIcCard();
            dto.setCommonCard(icCard);
            dto.setIcCard(null);
            dto.setSickId(null);
            if (StringUtils.isNotBlank(terminalDto.getIcCardInputSupportType())) {
                String icCardInputSupportType = terminalDto.getIcCardInputSupportType();
//                if ("common_card".equals(icCardInputSupportType)) {
//                    dto.setCommonCard(icCard);
//                } else if ("sick_id".equals(icCardInputSupportType)) {
//                    dto.setSickId(icCard);
//                    dto.setCommonCard(null);
//                } else if ("ic_card".equals(icCardInputSupportType)) {
//                    dto.setIcCard(icCard);
//                    dto.setCommonCard(null);
//                } else if ("inpatient_no".equals(icCardInputSupportType)) {
//                    dto.setInpatientNo(icCard);
//                } else if ("outpatient_no".equals(icCardInputSupportType)) {
//                    dto.setOutpatientNo(icCard);
//                } else if ("apply_no".equals(icCardInputSupportType)) {
//                    dto.setApplyNo(icCard);
//                } else if (ConstantsUtil.PatientDomainId.IDENTITY_CARD.getKey().equals(icCardInputSupportType)) {
//                    dto.setIdentityCard(icCard);
//                } else if ("mi_card".equals(icCardInputSupportType)) {
//                    dto.setMiCard(icCard);
//                } else if ("health_card".equals(icCardInputSupportType)) {
//                    dto.setHealthCard(icCard);
//                }
                if (ConstantsUtil.PatientDomainId.COMMON_CARD.getKey().equals(icCardInputSupportType)) {
                    dto.setCommonCard(icCard);
                } else if (ConstantsUtil.PatientDomainId.SICK_ID.getKey().equals(icCardInputSupportType)) {
                    dto.setSickId(icCard);
                    dto.setCommonCard(null);
                } else if (ConstantsUtil.PatientDomainId.IC_CARD.getKey().equals(icCardInputSupportType)) {
                    dto.setIcCard(icCard);
                    dto.setCommonCard(null);
                } else if (ConstantsUtil.PatientDomainId.INPATIENT_NO.getKey().equals(icCardInputSupportType)) {
                    dto.setInpatientNo(icCard);
                } else if (ConstantsUtil.PatientDomainId.OUTPATIENT_NO.getKey().equals(icCardInputSupportType)) {
                    dto.setOutpatientNo(icCard);
                } else if (ConstantsUtil.PatientDomainId.APPLY_NO.getKey().equals(icCardInputSupportType)) {
                    dto.setApplyNo(icCard);
                } else if (ConstantsUtil.PatientDomainId.IDENTITY_CARD.getKey().equals(icCardInputSupportType)) {
                    dto.setIdentityCard(icCard);
                } else if (ConstantsUtil.PatientDomainId.MI_CARD.getKey().equals(icCardInputSupportType)) {
                    dto.setMiCard(icCard);
                } else if (ConstantsUtil.PatientDomainId.HEALTH_CARD.getKey().equals(icCardInputSupportType)) {
                    dto.setHealthCard(icCard);
                }
            }
        } else {
            dto.setIcCard(null);
        }
    }


    @Override
    public JsonResult getScheduleExpireDate(ExamApplyDto dto) {
        return examApplyService.getScheduleExpireDate(dto);
    }

    /**
     * 查询申请单在对应的日期时段里面各个日期的可预约状态
     *
     * @param dto
     * @return
     */
    @Override
    public JsonResult getScheduleDates(ExamApplyDto dto) {
        if (StringUtils.isBlank(dto.getApplyNo())) {
            throw new BusinessException("applyNo不能为空");
        }
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        if (StringUtils.isNotBlank(dto.getStartDate())) {
            dto.setStartDate(dto.getStartDate());
        }
        if (StringUtils.isNotBlank(dto.getEndDate())) {
            dto.setEndDate(dto.getEndDate());
        }
        JsonResult jsonResult = examApplyService.getDateList(dto);
        if (jsonResult != null && jsonResult.getResult() != null) {
            for (ExamApplyDto examApplyDto : (List<ExamApplyDto>) jsonResult.getResult()) {
                examApplyDto.setValue(examApplyDto.getDate());
            }
        }
        return jsonResult;
    }


    /**
     * 申请单在某个日期内可以预约的时段列表
     *
     * @param dto
     * @return
     */
    @Override
    public JsonResult getEnabledScheduleApms(ExamApplyDto dto) {
        // ExamApplyDto dto = XmlUtil.xmlRequest2Bean(req, ExamApplyDto.class, "applyNo,scheduleDate,hospitalCode,mergeNo");
        if (StringUtils.isBlank(dto.getMergeNo())) {
            ExamApplyItemDto examApplyItemDto = new ExamApplyItemDto();
            examApplyItemDto.setMergeNos(CommonUtil.split2List(dto.getApplyNo()));
            String mergeNo = getMergeNos(examApplyItemDto);
            dto.setMergeNo(mergeNo);
        }
        JsonResult jsonResult = examApplyService.getApmList(dto);
        if (jsonResult.getResult() != null && "0".equals(jsonResult.getStatus())) {
            for (ExamApplyDto examApplyDto : (List<ExamApplyDto>) jsonResult.getResult()) {
                examApplyDto.setScheduleApm(examApplyDto.getApm());
            }
        }
        return jsonResult;
    }
}
